package com.example.zhangxinying.smratchatnew;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

/**
 * Created by zhangxinying on 16/9/8.
 */
public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.MyViewHolder> {
    private RecyclerView mRecyclerView;
    private View VIEW_FOOTER;
    private int TYPE_NORMAL = 1000;
    private int TYPE_FOOTER = 1002;
    private List<ShopBean> mylist = null;
    private Context mContex = null;


    public ShopAdapter(Context context, List<ShopBean> msglist) {
        this.mContex = context;
        this.mylist = msglist;
    }

    @Override
    public ShopAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder viewHolder;
        if (viewType == TYPE_FOOTER) {
            viewHolder = new MyViewHolder(VIEW_FOOTER,TYPE_NORMAL);
        } else {
            viewHolder = new MyViewHolder(LayoutInflater.from(mContex).inflate(R
                    .layout.recycler_item_shop, null, false),TYPE_NORMAL);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if (!isFooterView(position)) {
            holder.text_recycler_item_dp_title.setText(mylist.get(position).getDpTitle());
            holder.text_recycler_item_dp_distance.setText(mylist.get(position).getDpDistance());
            holder.text_recycler_item_dp_price.setText(mylist.get(position).getDpPrice());
        }
    }
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        try {
            if (mRecyclerView == null && mRecyclerView != recyclerView) {
                mRecyclerView = recyclerView;
            }
            ifGridLayoutManager();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        int count = (this.mylist == null ? 0 : this.mylist.size());
        if (VIEW_FOOTER != null) {
            count++;
        }

        return count;
    }

    @Override
    public int getItemViewType(int position) {
        if (isFooterView(position)) {
            return TYPE_FOOTER;
        } else {
            return TYPE_NORMAL;
        }
    }


    public void addFooterView(View footerView) {
        if (haveFooterView()) {
            throw new IllegalStateException("footerView has already exists!");
        } else {
            ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
            footerView.setLayoutParams(params);
            VIEW_FOOTER = footerView;
            ifGridLayoutManager();
            notifyItemInserted(getItemCount() - 1);
        }
    }

    private void ifGridLayoutManager() {
        if (mRecyclerView == null) return;
        final RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            final GridLayoutManager.SpanSizeLookup originalSpanSizeLookup =
                    ((GridLayoutManager) layoutManager).getSpanSizeLookup();
            ((GridLayoutManager) layoutManager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return (isFooterView(position)) ?
                            ((GridLayoutManager)layoutManager).getSpanCount() :
                            1;
                }
            });
        }
    }

    public boolean haveFooterView() {
        return VIEW_FOOTER != null;
    }


    private boolean isFooterView(int position) {
        return haveFooterView() && position == getItemCount() - 1;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView text_recycler_item_dp_price,text_recycler_item_dp_title,text_recycler_item_dp_distance;
        RelativeLayout rel_recycler_item_dp_bg;

        public MyViewHolder(View itemView,int type) {
            super(itemView);
            if(type == TYPE_NORMAL ) {
                text_recycler_item_dp_price = (TextView) itemView.findViewById(R.id.text_recycler_item_dp_price);
                text_recycler_item_dp_title = (TextView) itemView.findViewById(R.id.text_recycler_item_dp_title);
                text_recycler_item_dp_distance = (TextView) itemView.findViewById(R.id.text_recycler_item_dp_distance);
                rel_recycler_item_dp_bg = (RelativeLayout) itemView.findViewById(R.id.rel_recycler_item_dp_bg);
            }else {
                text_recycler_item_dp_price = (TextView) itemView.findViewById(R.id.rel_recycler_change_text_body);
            }
        }
    }
}

